package com.ego.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.commons.pojo.EasyUITree;
import com.ego.commons.pojo.EgoResult;
import com.ego.dubbo.service.TBContentCategoryDubboService;
import com.ego.pojo.TbContentCategory;
import com.ego.service.TBContentCategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TBContentCategoryServiceImpl implements TBContentCategoryService {


    @DubboReference
    private TBContentCategoryDubboService tbContentCategoryDubboService;

    @Override
    public List<EasyUITree> selectContentCategoryByParentId(long id) {
        return tbContentCategoryDubboService.selectContentCategoryByParentId(id);
    }

    @Override
    public EgoResult insert(TbContentCategory tbContentCategory) {


        try {

            //设置分类对象的时间，状态，是否父类
            Date now = new Date();

            tbContentCategory.setCreated(now);
            tbContentCategory.setUpdated(now);
            tbContentCategory.setStatus(1);
            tbContentCategory.setIsParent(false);

            int row = tbContentCategoryDubboService.insert(tbContentCategory);
            if(row == 1){
                return EgoResult.ok(tbContentCategory);
            }else {
                return EgoResult.error("新增内容分类失败");
            }
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }

    }

    @Override
    public EgoResult update(TbContentCategory tbContentCategory) {
        try {

            //设置分类对象的修改时间
            Date now = new Date();
            tbContentCategory.setUpdated(now);

            int row = tbContentCategoryDubboService.update(tbContentCategory);
            if(row == 1){
                return EgoResult.ok(tbContentCategory);
            }else {
                return EgoResult.error("修改内容分类失败");
            }
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }
    }

    @Override
    public EgoResult delete(long id) {

        try {
            int row = tbContentCategoryDubboService.deleteById(id);
            if(row == 1){
                return EgoResult.ok();
            }

        } catch (DaoException e) {
            return EgoResult.error(e.getMessage());
        }

        return EgoResult.error("删除内容分类失败！");
    }
}
